from StudentController import StudentController
from Student import Student


class StudentManagerView:
    """
        界面视图类
    """

    def __init__(self):
        self.__controller = StudentController()

    def __input__int(self, tip):
        while True:
            try:
                return int(input(tip))
            except:
                print('输入有误')

    def __input_student(self):
        name = input("请输入姓名：")
        age = self.__input__int("请输入年龄：")
        sex = input("请输入性别：")
        score = self.__input__int("请输入成绩：")
        student = Student(None, name, age, sex, score)
        self.__controller.add_student(student)

    def __output_students(self, list_target):
        """
            显示学生信息
            :param list_target:
            :return:
            """
        for student in list_target:
            print(student.id, student.name, student.age, student.sex, student.score)

    def __output_student_by_score(self):
        list_sort = self.__controller.sort_student()
        self.__output_students(list_sort)

    def __del_student(self):
        id = int(input('请输入指定编号的学生：'))
        is_success = self.__controller.delete_student_by_id(id)
        if is_success:
            print('删除成功')
        else:
            print('删除失败')

    def __display_menu(self):
        print("(1) 添加学生")
        print("(2) 显示学生")
        print("(3) 删除学生")
        print("(4) 修改学生")
        print("(5) 按照成绩降序排序")

    def __modify_student(self):
        id = int(input('请输入需要修改的学生编号：'))
        name = input('请输入姓名：')
        age = int(input('请输入年龄：'))
        score = int(input('请输入成绩：'))
        student = Student(id, name, age, score=score)
        if self.__controller.update_student(student):
            print('修改成功')
        else:
            print('更新失败 ')

    def __select_menu(self):
        number = input('请输入选项：')
        if number == "1":
            self.__input_student()
        elif number == "2":
            self.__output_students(self.__controller.stu_list)
        elif number == "3":
            self.__del_student()
        elif number == "4":
            self.__modify_student()
        elif number == "5":
            self.__output_student_by_score()
        else:
            pass

    def main(self):
        while True:
            self.__display_menu()
            self.__select_menu()


if __name__ == '__main__':
    view = StudentManagerView()
    view.main()
